package com.zichan360.routes;

import com.zichan360.service.CompanyService;
import com.zichan360.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.PUT;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

@Configuration
public class Routes {

	private CompanyService companyService;

	private UserService userService;

	@Autowired
	public Routes(CompanyService companyService, UserService userService) {
		this.companyService = companyService;
		this.userService = userService;
	}


	@Bean
	public RouterFunction<ServerResponse> 用户模块() {
		return route(GET("/api/user/{id}").and(accept(MediaType.ALL)), userService::getById)
				.and(route(GET("/api/user/").and(accept(MediaType.ALL)), userService::list))
				.and(route(POST("/api/user").and(accept(MediaType.APPLICATION_JSON)), userService::createOrUpdate))
				.and(route(PUT("/api/user").and(accept(MediaType.APPLICATION_JSON)), userService::createOrUpdate));
	}


	@Bean
	public RouterFunction<?> 模块2() {
		return route(GET("/api/user").and(accept(MediaType.APPLICATION_JSON)), companyService::list)
				.and(route(GET("/api/user/{id}").and(accept(MediaType.APPLICATION_JSON)), companyService::getById));

	}

}
